import { TaskStatus } from './../task-status.enum';
import { PipeTransform, BadRequestException } from "@nestjs/common";

export class TaskStatusValidationPipe implements PipeTransform {
  readonly allowedStatuses = [
    TaskStatus.OPEN,
    TaskStatus.IN_PROGRESS,
    TaskStatus.DONE
  ]

  transform(value: any) {
    value = value.toUpperCase()
    console.log('value', value)
    if (!this.isStatusValid(value)) {
      throw new BadRequestException(`${value} is an invalid status`)
    }
    return value
  }
  
  private isStatusValid(status: any): boolean {
    const idx = this.allowedStatuses.indexOf(status)
    return idx !== -1
  }
}